/*
 * @file gkt_widget_inputbox.h
 */

#ifndef GKT_WIDGET_INPUTBOX_H
#define GKT_WIDGET_INPUTBOX_H

#include "gkt_types.h"

#ifdef __cplusplus
extern "C" {
#endif

#if defined(GKT_CONFIG_SCREEN_ENABLE) && GKT_CONFIG_SCREEN_ENABLE

/* attr of inputbox desc
 * min_size : 
 * max_size : 
 * password : normal textbox or password box
 */
#define GKT_WIDGET_INPUTBOX_BASE_FLAGS	\
	(GKT_WIDGET_F_ALIGN_LEFT | GKT_WIDGET_F_BORDER | GKT_WIDGET_F_FOCUSABLE)
#define GKT_WIDGET_INPUTBOX_FLAGS(_flags)	\
	(((_flags) & (~GKT_WIDGET_F_ALIGN_HMASK)) | GKT_WIDGET_INPUTBOX_BASE_FLAGS)

#define GKT_WIDGET_INPUTBOX_ATTR_MINSIZE_SHIFT	0
#define GKT_WIDGET_INPUTBOX_ATTR_MINSIZE_BITS	8
#define GKT_WIDGET_INPUTBOX_ATTR_MINSIZE_MASK	\
	((1U << GKT_WIDGET_INPUTBOX_ATTR_MINSIZE_BITS) - 1)

#define GKT_WIDGET_INPUTBOX_ATTR_MAXSIZE_SHIFT	\
	(GKT_WIDGET_INPUTBOX_ATTR_MINSIZE_SHIFT + GKT_WIDGET_INPUTBOX_ATTR_MINSIZE_BITS)
#define GKT_WIDGET_INPUTBOX_ATTR_MAXSIZE_BITS	8
#define GKT_WIDGET_INPUTBOX_ATTR_MAXSIZE_MASK	\
	((1U << GKT_WIDGET_INPUTBOX_ATTR_MAXSIZE_BITS) - 1)

#define GKT_WIDGET_INPUTBOX_ATTR_PASSWORD_SHIFT	\
	(GKT_WIDGET_INPUTBOX_ATTR_MAXSIZE_SHIFT + GKT_WIDGET_INPUTBOX_ATTR_MAXSIZE_BITS)
#define GKT_WIDGET_INPUTBOX_ATTR_PASSWORD	\
	(1U << GKT_WIDGET_INPUTBOX_ATTR_PASSWORD_SHIFT)
#define GKT_WIDGET_INPUTBOX_ATTR_NORMAL	0

#define GKT_WIDGET_INPUTBOX_ATTR(min_size, max_size, password)	\
	((((min_size) & GKT_WIDGET_INPUTBOX_ATTR_MINSIZE_MASK) << GKT_WIDGET_INPUTBOX_ATTR_MINSIZE_SHIFT)	\
		| (((max_size) & GKT_WIDGET_INPUTBOX_ATTR_MAXSIZE_MASK) << GKT_WIDGET_INPUTBOX_ATTR_MAXSIZE_SHIFT)	\
		| GKT_WIDGET_INPUTBOX_ATTR_##password)

/* inputbox desc */
#define GKT_WIDGET_INPUTBOX_TEXT_DESC(_flags, _font_size, \
			_x, _y, _w, _h, _min_size, _max_size)	\
	GKT_WIDGET_DESC(INPUTBOX, GKT_WIDGET_INPUTBOX_FLAGS(_flags), \
				_font_size, \
				GKT_WIDGET_FG_COLOR_DEFAULT, \
				GKT_WIDGET_BG_COLOR_DEFAULT, \
				_x, _y, _w, _h, \
				GKT_WIDGET_INPUTBOX_ATTR(_min_size, _max_size, NORMAL))

#define GKT_WIDGET_INPUTBOX_PWD_DESC(_flags, _font_size, \
			_x, _y, _w, _h, _min_size, _max_size)	\
	GKT_WIDGET_DESC(INPUTBOX, GKT_WIDGET_INPUTBOX_FLAGS(_flags), \
				_font_size, \
				GKT_WIDGET_FG_COLOR_DEFAULT, \
				GKT_WIDGET_BG_COLOR_DEFAULT, \
				_x, _y, _w, _h, \
				GKT_WIDGET_INPUTBOX_ATTR(_min_size, _max_size, PASSWORD))

typedef union _gkt_widget_inputbox_attr {
	struct {
		uint8_t	min_size;
		uint8_t	max_size;
		uint8_t	password;
		uint8_t	reserved[1];
	};
	uint32_t	value;
} gkt_widget_inputbox_attr_u;

#define GKT_WIDGET_INPUTBOX_DATA_DEFAULT_SIZE	8
typedef struct _gkt_widget_inputbox_data {
	uint32_t	size;
	uint32_t	focus_index;
	char	data[GKT_WIDGET_INPUTBOX_DATA_DEFAULT_SIZE];
} gkt_widget_inputbox_data_s;

#else

#define GKT_WIDGET_INPUTBOX_BASE_FLAGS	GKT_WIDGET_F_FOCUSABLE
#define GKT_WIDGET_INPUTBOX_FLAGS(_flags)	\
	((_flags) | GKT_WIDGET_INPUTBOX_BASE_FLAGS)

#define GKT_WIDGET_INPUTBOX_ATTR_MINSIZE_SHIFT	0
#define GKT_WIDGET_INPUTBOX_ATTR_MINSIZE_BITS	8
#define GKT_WIDGET_INPUTBOX_ATTR_MINSIZE_MASK	\
	((1U << GKT_WIDGET_INPUTBOX_ATTR_MINSIZE_BITS) - 1)

#define GKT_WIDGET_INPUTBOX_ATTR_MAXSIZE_SHIFT	\
	(GKT_WIDGET_INPUTBOX_ATTR_MINSIZE_SHIFT + GKT_WIDGET_INPUTBOX_ATTR_MINSIZE_BITS)
#define GKT_WIDGET_INPUTBOX_ATTR_MAXSIZE_BITS	8
#define GKT_WIDGET_INPUTBOX_ATTR_MAXSIZE_MASK	\
	((1U << GKT_WIDGET_INPUTBOX_ATTR_MAXSIZE_BITS) - 1)

#define GKT_WIDGET_INPUTBOX_ATTR(min_size, max_size)	\
	((((min_size) & GKT_WIDGET_INPUTBOX_ATTR_MINSIZE_MASK) << GKT_WIDGET_INPUTBOX_ATTR_MINSIZE_SHIFT)	\
		| (((max_size) & GKT_WIDGET_INPUTBOX_ATTR_MAXSIZE_MASK) << GKT_WIDGET_INPUTBOX_ATTR_MAXSIZE_SHIFT))

/* inputbox desc */
#define GKT_WIDGET_INPUTBOX_DESC(_flags, _min_size, _max_size)	\
	GKT_WIDGET_DESC(INPUTBOX, GKT_WIDGET_INPUTBOX_FLAGS(_flags), \
				GKT_WIDGET_INPUTBOX_ATTR(_min_size, _max_size))

typedef union _gkt_widget_inputbox_attr {
	struct {
		uint8_t	min_size;
		uint8_t	max_size;
		uint8_t	reserved[2];
	};
	uint32_t	value;
} gkt_widget_inputbox_attr_u;

#define GKT_WIDGET_INPUTBOX_DATA_DEFAULT_SIZE	12
typedef struct _gkt_widget_inputbox_data {
	uint32_t	size;
	char	data[GKT_WIDGET_INPUTBOX_DATA_DEFAULT_SIZE];
} gkt_widget_inputbox_data_s;
#endif

int gkt_widget_inputbox_event_handler(
			gkt_widget_s *widget, 
			uint32_t major_type, uint32_t minor_type, 
			uint32_t data);

#ifdef __cplusplus
}
#endif

#endif	/* GKT_WIDGET_INPUTBOX_H */

